package com.googlecode.continuity.core.client;

import java.util.*;
import java.io.Serializable;

import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * A Message represents a text that has an author, a creation date and possibly replies.  As the most basic text storage
 * class, it is used in various places throughout the framework, usually in an extended manner.
 * <p/>
 * TODO: rename?
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class Message implements IsSerializable, Serializable {
// ------------------------------ FIELDS ------------------------------

  /**
   * @gwt.typeArgs <com.googlecode.continuity.core.client.Message>
   */
  private Set replies;
  private int id;
  // TODO: make content complex, i.e. rich text etc.
  private String content;
  private ContinuityUser author;
  private Date creationDate;

// --------------------- GETTER / SETTER METHODS ---------------------

  /**
   * Returns the author of this message.
   *
   * @return the author of this message
   */
  public ContinuityUser getAuthor() {
    return author;
  }

  /**
   * Sets a new author for this message.
   *
   * @param author new author for this message
   */
  public void setAuthor(ContinuityUser author) {
    this.author = author;
  }

  /**
   * Returns the date and time at which this message was created.
   *
   * @return date and time at which this message was created
   */
  public Date getCreationDate() {
    return creationDate;
  }

  /**
   * Sets a new date and time at which this message was created.
   *
   * @param creationDate date and time at which this message was created
   */
  public void setCreationDate(Date creationDate) {
    this.creationDate = creationDate;
  }

  /**
   * Returns the unique id of this message.
   *
   * @return unique id of this message
   */
  public int getId() {
    return id;
  }

  /**
   * Sets a new unique id for this message.
   *
   * @param id new unique id for this message
   */
  public void setId(int id) {
    this.id = id;
  }

  /**
   * Returns this message's text content.
   *
   * @return this message's text content
   */
  public String getContent() {
    return content;
  }

  /**
   * Sets a new text content for this message.
   *
   * @param content new text content for this message
   */
  public void setContent(String content) {
    this.content = content;
  }

  /**
   * Returns the set of replies to this message.
   *
   * @return replies to this message
   */
  public Set getReplies() {
    if (replies == null) {
      replies = new HashSet();
    }
    return replies;
  }

  /**
   * Sets a new set of replies for this message.
   *
   * @param replies new set of replies for this message
   */
  public void setReplies(Set replies) {
    this.replies = replies;
  }

// -------------------------- OTHER METHODS --------------------------

  /**
   * Adds a reply to this message.
   *
   * @param reply new reply to be added
   */
  public void addReply(Reply reply) {
    getReplies().add(reply);
  }
}
